const util = require('../../utils/util.js')
Page({
  data: {
    taskList: [],
    page: 1,
    pageSize: 10,
    isLast: false
  },
  onLoad: function (options) {
    this.getTaskList()
  },
  getTaskList: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.cloud.callFunction({
      name: 'getTaskList',
      data: {
        page: this.data.page,
        pageSize: this.data.pageSize,
        examDate: util.date(),
        openid: wx.getStorageSync('openid')
      },
      success: function(res) {
        wx.hideLoading()
        wx.stopPullDownRefresh()
        that.setData({
          taskList: that.data.taskList.concat(res.result)
        })
        if (res.result.length < that.data.pageSize) {
          that.setData({
            isLast: true
          })
        }
      },
      fail: function(res) {
        console.log(res)
      }
    })
  },
  onReachBottom: function () {
    if (this.data.isLast === false) {
      this.setData({
        page: this.data.page + 1
      })
      this.getTaskList()
    } else {
      wx.showToast({
        title: '已经没有数据了...',
        icon: 'none'
      })
    }
  },
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      taskList: [],
      isLast: false
    })
    this.getTaskList()
  },
  subscribe: function(e) {
    var that = this
    wx.requestSubscribeMessage({
      tmplIds: ['Ss6dzyGWZYkFf5lwJofeeht0l1dt8nQJmBdwD9zS8Z4'],
      success: function (res) {
        if (res['Ss6dzyGWZYkFf5lwJofeeht0l1dt8nQJmBdwD9zS8Z4'] == 'reject') {
          wx.showModal({
            title: '警告',
            content: '取消订阅消息将不能接收平台的通知提醒',
            showCancel: false
          })
        } else {
          wx.showLoading({
            title: '订阅中',
          })
          wx.cloud.callFunction({
            name: 'getExamById',
            data: {
              _id: e.currentTarget.dataset._id
            },
            success: function(res) {
              wx.cloud.callFunction({
                name: 'setSubscribe',
                data: {
                  openid: wx.getStorageSync('openid'),
                  examId: e.currentTarget.dataset._id,
                  created_at: util.datetime()
                },
                success: function(res) {
                  wx.hideLoading()
                  wx.showToast({
                    title: '订阅成功',
                    success: function() {
                      that.data.taskList[e.currentTarget.dataset.index].isSubscribe = 1
                      that.setData({
                        taskList: that.data.taskList
                      })
                    }
                  })
                }
              })
            },
            fail: function(res) {
              wx.hideLoading()
              wx.showToast({
                title: '考试任务不存在或者已删除',
                icon: 'none'
              })
            }
          })
        }
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },
  unSubscribe: function(e) {
    wx.showLoading({
      title: '取消订阅',
    })
    var that = this
    wx.cloud.callFunction({
      name: 'setUnSubscribe',
      data: {
        openid: wx.getStorageSync('openid'),
        examId: e.currentTarget.dataset._id,
      },
      success: function(res) {
        wx.hideLoading()
        wx.showToast({
          title: '取消成功',
          success: function () {
            that.data.taskList[e.currentTarget.dataset.index].isSubscribe = 0
            that.setData({
              taskList: that.data.taskList
            })
          }
        })
      },
      fail: function(res) {
        console.log(res)
      }
    })
  }
})